using System.Collections.Generic;
using UnityEngine;

public class GameSound : MonoBehaviour
{
    void Start()
    {
        BuildAudioLibrary();
    }

    void BuildAudioLibrary()
	{
        Audios.Clear();

        var sources = GetComponentsInChildren<AudioSource>();
        foreach (var source in sources)
        {
            var clipName = source.clip.name;
            if (!Audios.ContainsKey(clipName))
            {
                Audios.Add(clipName, source);
            }

            if (!AudioVolumeCache.ContainsKey(clipName))
			{
                AudioVolumeCache.Add(clipName, source.volume);
			}
        }
    }

	Dictionary<string, AudioSource> Audios => audios ?? (audios = new Dictionary<string, AudioSource>());
    Dictionary<string, AudioSource> audios;

    Dictionary<string, float> AudioVolumeCache => audioVolumeCache ?? (audioVolumeCache = new Dictionary<string, float>());
    Dictionary<string, float> audioVolumeCache;

    public void PlayAudio(string clipName)
	{
        if (Audios.ContainsKey(clipName))
		{
            Audios[clipName].Play();
		}
	}

    public void SetMasterVolume(float volumePercentage)
	{
        foreach (var audio in Audios)
		{
            if (AudioVolumeCache.ContainsKey(audio.Key))
			{
                audio.Value.volume = AudioVolumeCache[audio.Key] * volumePercentage;
            }
		}
	}
}
